#include "widget.h"
#include "ui_widget.h"
#include "QProcess"
#include "QDebug"
#include <QSettings>
#include <QDir>
#include <QDateTime>

#define LIGHT_H 255
#define LIGHT_L 0

uint8_t pow_on_time_h,pow_on_time_m,pow_off_time_h,pow_off_time_m,fun_enable,system_on;
extern QString show_flag;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    timer_calendar = new QTimer(this);//new个对象
    connect(timer_calendar,SIGNAL(timeout()),this,SLOT(timer_Update()));//timeout超时事件
    timer_calendar->start(1000);//每隔一秒调用一次槽函数
    QString currentPath = QDir::currentPath()+"/parm.ini";
    system_on=1;
    settings = new QSettings(currentPath,QSettings::IniFormat);
    m_setting_init();



}

Widget::~Widget()
{
    delete ui;
}

void Widget::timer_Update()
{
    QDateTime time = QDateTime::currentDateTime();
    //年-月-日 时：分：秒 星期
    QString str = time.toString("hh:mm:ss");
    ui->time_show->setText(str);
    if(fun_enable)
    {

        uint8_t c_hour,c_min;
        c_hour = time.time().hour();
        c_min = time.time().minute();

        if(pow_on_time_h<pow_off_time_h)
        {
            if(c_hour<pow_on_time_h && c_min<pow_on_time_m)
            {
                if(system_on)
                {
                    set_light_fun(LIGHT_L);
                    system_on=0;
                }
            }
            else if(c_hour>=pow_off_time_h && c_min>=pow_off_time_m )
            {
                if(system_on)
                {
                    set_light_fun(LIGHT_L);
                    system_on=0;
                }
            }
            else {
                if(system_on==0)
                {
                    set_light_fun(LIGHT_H);
                    system_on=1;
                }
            }
        }
        else if(pow_on_time_h==pow_off_time_h)
        {
            if(pow_on_time_m<pow_off_time_m)
            {
                if(c_hour<pow_on_time_h && c_min<pow_on_time_m)
                {
                    if(system_on)
                    {
                        set_light_fun(LIGHT_L);
                        system_on=0;
                    }
                }
                else if(c_hour>=pow_off_time_h && c_min>=pow_off_time_m )
                {
                    if(system_on)
                    {
                        set_light_fun(LIGHT_L);
                        system_on=0;
                    }
                }
                else {
                    if(system_on==0)
                    {
                        set_light_fun(LIGHT_H);
                        system_on=1;
                    }
                }
            }
            else if(pow_on_time_m>pow_off_time_m)
            {
                if(c_hour<pow_off_time_h && c_min<pow_off_time_m)
                {
                    if(system_on==0)
                    {
                        set_light_fun(LIGHT_H);
                        system_on=1;
                    }
                }
                else if(c_hour>=pow_on_time_h && c_min>=pow_on_time_m )
                {
                    if(system_on==0)
                    {
                        set_light_fun(LIGHT_H);
                        system_on=1;
                    }
                }
                else {
                    if(system_on)
                    {
                        set_light_fun(LIGHT_L);
                        system_on=0;
                    }
                }
            }
        }
        else if(pow_on_time_h>=pow_off_time_h) {
            if(c_hour<pow_off_time_h && c_min<pow_off_time_m)
            {
                if(system_on==0)
                {
                    set_light_fun(LIGHT_H);
                    system_on=1;
                }
            }
            else if(c_hour>=pow_on_time_h && c_min>=pow_on_time_m )
            {
                if(system_on==0)
                {
                    set_light_fun(LIGHT_H);
                    system_on=1;
                }
            }
            else {
                if(system_on)
                {
                    set_light_fun(LIGHT_L);
                    system_on=0;
                }
            }
        }
    }
}

void Widget::m_setting_init()
{
    QString str_power;
    str_power=settings->value("POWER_ON_H").toString();
    if(str_power == NULL)
    {
      pow_on_time_h= 0;
      str_power = pow_on_time_h;
      settings->setValue("POWER_ON_H",pow_on_time_h);
    }
    else {
        pow_on_time_h=str_power.toInt();
        ui->power_h->setValue(pow_on_time_h);
    }

    str_power=settings->value("POWER_ON_M").toString();
    if(str_power == NULL)
    {
      pow_on_time_m= 0;
      str_power = pow_on_time_m;
      settings->setValue("POWER_ON_M",pow_on_time_m);
    }
    else {
        pow_on_time_m=str_power.toInt();
        ui->power_m->setValue(pow_on_time_m);
    }

    str_power=settings->value("POWER_OFF_H").toString();
    if(str_power == NULL)
    {
      pow_off_time_h= 0;
      settings->setValue("POWER_OFF_H",pow_off_time_h);
    }
    else {
        pow_off_time_h=str_power.toInt();
        ui->power_off_h->setValue(pow_off_time_h);

    }
    str_power=settings->value("POWER_OFF_M").toString();
    if(str_power == NULL)
    {
      pow_off_time_m= 0;
      settings->setValue("POWER_OFF_M",pow_off_time_m);
    }
    else {
        pow_off_time_m=str_power.toInt();
        ui->power_off_m->setValue(pow_off_time_m);
    }
    str_power=settings->value("POWER_ENABLE").toString();
    if(str_power == NULL)
    {
      fun_enable= 0;
      settings->setValue("POWER_ENABLE",fun_enable);
    }
    else {
        fun_enable=str_power.toInt();
        ui->btn_enable->setChecked(fun_enable);
    }
}


void Widget::set_light_fun(uint8_t brightness)
{
    QProcess setBright(this);// 进程函数，调用外部指令窗口
    setBright.start("cat /sys/class/backlight/backlight/brightness \n"); //获取当前亮度值
    setBright.waitForFinished(); // 等待命令执行完成

    QString strCmd = QString("sudo /bin/sh -c  \"echo %1 > /sys/class/backlight/backlight/brightness\"").arg(brightness);
    //执行指令
    setBright.start(strCmd.toLocal8Bit().data());   //convert QString to char
    setBright.waitForFinished(1000);// 等待指令执行完毕
    QString strErrors = setBright.readAllStandardError();// 获取返回值
    qDebug()<<strErrors;
}

void Widget::on_btn_save_clicked()
{
    uint8_t power_val,power_set;
    power_val=ui->power_h->value();
    power_set=settings->value("POWER_ON_H").toInt();
    if(power_val != power_set)
    {
      pow_on_time_h= power_val;
      settings->setValue("POWER_ON_H",pow_on_time_h);
    }
    power_val=ui->power_m->value();
    power_set=settings->value("POWER_ON_M").toInt();
    if(power_set != power_val)
    {
      pow_on_time_m= power_val;
      settings->setValue("POWER_ON_M",pow_on_time_m);
    }
    power_val=ui->power_off_h->value();
    power_set=settings->value("POWER_OFF_H").toInt();
    if(power_set != power_val)
    {
      pow_off_time_h= power_val;
      settings->setValue("POWER_OFF_H",pow_off_time_h);
    }

    power_val=ui->power_off_m->value();
    power_set=settings->value("POWER_OFF_M").toInt();
    if(power_set != power_val)
    {
      pow_off_time_m= power_val;
      settings->setValue("POWER_OFF_M",pow_off_time_m);
    }

    if(ui->btn_enable->isChecked())
        power_val =1;
    else power_val=0;
    power_set=settings->value("POWER_ENABLE").toInt();
    if(power_set != power_val)
    {
      fun_enable= power_val;
      settings->setValue("POWER_ENABLE",fun_enable);
    }

}
